package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

public class Tortue {
	private double convDegGrad = 0.0174533;
	private int x, y;	// les coordonnees de la tortue
	private int angle;
	private ZoneDessin feuille; // la feuille de dessin sur laquelle dessine la tortue
	private boolean crayon = true;
	int coul;
	protected static final int rp=10,rb=5;
	private int vitesse = 20;
	
	public void setColor(int n){coul = n;}
	public int getColor() {return coul;}
	
	
	public Tortue(ZoneDessin f){
		feuille = f;
		feuille.addTortue(this);
		reset();
	}
	
	public void reset()
  	{	// on initialise la position de la tortue
		x = feuille.getDim().width/2;
		y = feuille.getDim().height/2;
		angle = -90;
		crayon = true;
  	}
	
	public void baisser(){
		crayon = true;
	}
	
	public void lever(){
		crayon = false;
	}
	
	protected void drawTurtle (Graphics g){
	      //Calcule les 3 coins du triangle a partir de
	      // la position de la tortue p
		  Point p = new Point(x,y);
		  Polygon arrow = new Polygon();

	      //Calcule des deux bases
	      //Angle de la droite
	      double theta=convDegGrad*(-angle);
	      //Demi angle au sommet du triangle
	      double alpha=Math.atan( (float)rb / (float)rp );
	      //Rayon de la fleche
	      double r=Math.sqrt( rp*rp + rb*rb );
	      //Sens de la fleche

	      //Pointe
		  Point p2=new Point((int) Math.round(p.x+r*Math.cos(theta)),
							 (int) Math.round(p.y-r*Math.sin(theta)));
		  arrow.addPoint(p2.x,p2.y);
		  arrow.addPoint((int) Math.round( p2.x-r*Math.cos(theta + alpha) ),
			  (int) Math.round( p2.y+r*Math.sin(theta + alpha) ));

	      //Base2
		  arrow.addPoint((int) Math.round( p2.x-r*Math.cos(theta - alpha) ),
			  (int) Math.round( p2.y+r*Math.sin(theta - alpha) ));

		  arrow.addPoint(p2.x,p2.y);
		  g.setColor(Color.green);
		  g.fillPolygon(arrow);
	    }
	
	Color decodeColor(int c) {
  		switch(c) {
  			case 0: return(Color.black);
  			case 1: return(Color.blue);
  			case 2: return(Color.cyan);
  			case 3: return(Color.darkGray);
  			case 4: return(Color.red);
  			case 5: return(Color.green);
  			case 6: return(Color.lightGray);
  			case 7: return(Color.magenta);
  			case 8: return(Color.orange);
  			case 9: return(Color.gray);
  			case 10: return(Color.pink);
  			case 11: return(Color.yellow);
  			default : return(Color.black);
		}
  	}
	
	// avancer de n pas
	public void avancer(int dist)
	{	Graphics g = feuille.getImageGraphics();

		int newX = (int) Math.round(x+Math.cos(convDegGrad*angle));
		int newY = (int) Math.round(y+Math.sin(convDegGrad*angle));
	
		for(int i=1; i<=dist; i++){
			newX = (int) Math.round(x+Math.cos(convDegGrad*angle));
			newY = (int) Math.round(y+Math.sin(convDegGrad*angle));
		
			try {
				Thread.sleep(vitesse);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (crayon) {
				g.setColor(decodeColor(coul));
				g.drawLine(x,y,newX,newY);
				//g.drawLine(x,y,newX,newY);

			}
			
			x = newX;
			y = newY;
			//if (getShowTurtle())
			//  this.drawTurtle(g);
			// System.out.println("dir: " + dir);
			feuille.drawIt();
		}
	

		
		
	}

	// aller � droite
	public void droite(int ang)
	{
		angle = (angle + ang) % 360;
		//if (getShowTurtle())
		//  this.drawTurtle(feuille.getGraphics());
		//System.out.println("dir: " + dir);
		feuille.drawIt();
	}

	// aller � gauche
	public void gauche(int ang)
	{
		angle = (angle - ang) % 360;
		//if (getShowTurtle())
		//  this.drawTurtle(feuille.getGraphics());
		//System.out.println("dir: " + dir);
		feuille.drawIt();
	}
	
	
	/*

	
	public Point getDepart() {
		return depart;
	}

	public void setDepart(Point p) {
		this.depart = p;
	}

	public void setDepart(int x, int y) {
		this.depart.x = x;
		this.depart.y = y;
	}
	
	public Point getArrivee() {
		return arrivee;
	}

	public void setArrivee(Point p) {
		this.arrivee = p;
	}
	
	public void setArrivee(int x, int y) {
		this.arrivee.x = x;
		this.arrivee.y = y;
	}



*/
	
	
	
	/** quelques classiques */
/*
	void carre() {
		for (int i=0;i<4;i++) {
			avancer(100);
			droite(90);
		}
	}

	void poly(int n, int a) {
		for (int j=0;j<a;j++) {
			avancer(n);
			droite(360/a);
		}
	}

	void spiral(int n, int k, int a) {
		for (int i = 0; i < k; i++) {
			couleur(coul+1);
			avancer(n);
			droite(360/a);
			n = n+1;
		}
	}
*/

}
